package com.qf.smsplatfrom.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.qf.smsplatfrom.annotations.DescribeAnno;
import com.qf.smsplatfrom.controller.swagger.MyProceedingJoinPoint;
import com.qf.smsplatfrom.dto.R;
import com.qf.smsplatfrom.pojo.TAdminUser;
import com.qf.smsplatfrom.pojo.TSmsTemplate;
import com.qf.smsplatfrom.service.TSmsTemplateService;
import io.swagger.models.auth.In;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/template")
public class TSmsTemplateController {

    private TSmsTemplateService tSmsTemplateService;

    @Autowired
    public void settSmsTemplateService(TSmsTemplateService tSmsTemplateService) {
        this.tSmsTemplateService = tSmsTemplateService;
    }

    @PostMapping("/template")
    public R addTemplate(@RequestBody TSmsTemplate tSmsTemplate, HttpSession session) {

        Object user = session.getAttribute("user");
        if (user != null) {
            long id = ((TAdminUser) user).getId();
            long type = ((TAdminUser) user).getType();
            tSmsTemplate.setCreater(id);
            tSmsTemplate.setOwntype(type);
        }
        tSmsTemplateService.addTemplate(tSmsTemplate);
        return R.setOk();
    }

    @GetMapping("/template")
    public R showAllTemplate(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize) {
        PageInfo<TSmsTemplate> pageInfo = tSmsTemplateService.showAll(pageNum, pageSize);

        return R.setOk(pageInfo);
    }

    @GetMapping("/showBy")
    @DescribeAnno("根据用户类型状态进行查询")
    public R showByMultiParam(@RequestBody TSmsTemplate tSmsTemplate, @RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "5")Integer pageSize) throws IllegalAccessException, InstantiationException {
        PageInfo<TSmsTemplate> smsTemplates = tSmsTemplateService.findByMultiParam(tSmsTemplate,pageNum,pageSize);
        return R.setOk(smsTemplates);
    }


}
